import 'package:bxultimate/app/router/router_name.dart';
import 'package:bxultimate/app/theme/gaps.dart';
import 'package:bxultimate/data/request/user_request.dart';
import 'package:bxultimate/data/store/auth_store.dart';
import 'package:bxultimate/domain/auth/login_token.dart';
import 'package:bxultimate/shared/util/util_location.dart';
import 'package:bxultimate/shared/util/util_mk.dart';
import 'package:bxultimate/shared/util/util_mqtt.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import 'state.dart';

class AuthLoginController extends GetxController {
  final AuthLoginState state = AuthLoginState();

  final TextEditingController nameController = TextEditingController();
  final TextEditingController passwordController = TextEditingController();
  final FocusNode nodeText1 = FocusNode();
  final FocusNode nodeText2 = FocusNode();

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    nameController.addListener(_verify);
    passwordController.addListener(_verify);
  }

  /**
   * 登录检查
   */
  void loginCheck() {
    print(state.checkboxIsCheck);
    if (state.checkboxIsCheck) {
      login();
      return;
    }
    TextStyle style =
        TextStyle(fontSize: 16, color: TDTheme.of(Get.context!).brandColor5);
    UtilMk.openBottomSheet(
        230,
        "请阅读并同意以下条款",
        SafeArea(
            child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Gaps.vGap24,
            Row(mainAxisAlignment: MainAxisAlignment.center, children: [
              GestureDetector(
                onTap: () =>
                    {Get.toNamed(RouterName.userSystemSettingUserAgreement)},
                child: Text(
                  "《用户协议》",
                  style: style,
                ),
              ),
              GestureDetector(
                onTap: () => {Get.toNamed(RouterName.userSystemSettingPrivacy)},
                child: Text(
                  "《隐私政策》",
                  style: style,
                ),
              ),
            ]),
            Gaps.vGap24,
            Padding(
              padding: EdgeInsets.symmetric(horizontal: 20),
              child: TDButton(
                theme: TDButtonTheme.primary,
                onTap: continueStep,
                text: "同意并登录",
                isBlock: true,
              ),
            )
          ],
        )));
  }

  void continueStep() {
    state.checkboxIsCheck = true;
    Get.back();
    login();
  }

  Future<void> login() async {
    SmartDialog.showLoading(msg: '登录中...');

    try{
      LoginToken loginTokenEntity = await UserRequest.loginByPassword(
          nameController.text, passwordController.text);
      await AuthStore.to.login(loginTokenEntity.accessToken!);
      SmartDialog.showNotify(msg: '登录成功', notifyType: NotifyType.success);

      UtilLocation.getLocation();
      // mqtt
      UtilMqtt().connect();

      Get.back();
    }catch (e){
      SmartDialog.dismiss();
      SmartDialog.showNotify(msg: '手机号或密码错误', notifyType: NotifyType.failure);
    }
  }

  void _verify() {
    final String name = nameController.text;
    final String password = passwordController.text;
    if (name.isEmpty || name.length < 11) {
      state.clickable = false;
    } else if (password.isEmpty || password.length < 6) {
      state.clickable = false;
    } else {
      state.clickable = true;
    }
  }

  void toSmsLogin() {
    Get.offAndToNamed(RouterName.userLoginSms);
  }

  checkBoxCheck(bool checked) {
    state.checkboxIsCheck = checked;
  }

  void wxLogin() {}
}
